package com.evzgaga.evzrecon.annotations.runtime;

import com.evzgaga.evzrecon.annotations.ChatCommandHandler;
import com.evzgaga.evzrecon.event.ChatCommandEvent;
import com.evzgaga.evzrecon.util.CallbackInfo;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Evzgaga
 */
public class ChatCommandAnnotationAnalyzer extends AbstractMethodAnnotationRuntimeAnalyzer {

    @Override
    protected void postprocessCallbackInfo(CallbackInfo info, Map<String, CallbackInfo> callbacks) {
        if (hasExpectedChatCommandHandlerSignature(info.method)) {
            ChatCommandHandler annotation = info.method.getAnnotation(ChatCommandHandler.class);
            for (String callbackName : annotation.commands()) {
                callbacks.put(callbackName, info);
            }
        } else {
            Logger.getLogger(getClass().getName()).log(Level.INFO, "Method {0} has not the required signature and has "
                    + "been ignored for chat command handling", info.method.getName());
        }
    }

    protected boolean hasExpectedChatCommandHandlerSignature(Method method) {
        Class<?>[] parameters = method.getParameterTypes();
        
        return ((parameters.length == 1) && (parameters[0] == ChatCommandEvent.class));
    }
}
